package com.lxk.quartz;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @author JohnSoybean
 * @create 2020-04-22 23:26
 * @description 随项目启动定时器配置类
 */
@Configuration
public class QuartzConfig {

    /**
     * 编写其他接口时，暂时关闭定时器
     */
    Date nowTime = new Date();

    /**
     * 排队过期进程监听任务 实体
     * @return
     */
    @Bean
    public JobDetail queueExpireJobDetail() {
        JobDetail jobDetail = JobBuilder.newJob(QueueExpireJob.class)
                .withIdentity("queueExpireJob", "queueExpireJobGroup")
                .storeDurably()
                .build();
        return jobDetail;
    }

    /**
     * 排队过期监听任务 触发器
     * 服务启动30秒后触发，每隔 1秒触发一次
     * @return
     */
    @Bean
    public Trigger queueExpireTrigger(){
        Date startTime = new Date(nowTime.getTime() + 30000);
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(queueExpireJobDetail())
                .withIdentity("queueExpireTrigger", "queueExpireTriggerGroup")
                .startAt(startTime)
                .withSchedule(CronScheduleBuilder.cronSchedule("*/1 * * * * ? *"))
                .build();
        return trigger;
    }

}
